package com.yojcodesendbox.codesendbox.impl;

import com.yojcodesendbox.codesendbox.CodeSandbox;
import com.yojcodesendbox.codesendbox.ExecuteBase;
import com.yojcodesendbox.codesendbox.ExecuteCodeTemplate;
import com.yojcodesendbox.constant.FileConstant;
import com.yojcodesendbox.model.ExecuteCodeRequest;
import com.yojcodesendbox.model.ExecuteCodeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
public class JavaNativeCodeSendbox extends CodeSandbox implements ExecuteBase {

    @Autowired
    ExecuteCodeTemplate executeCodeTemplate;

    @Resource
    FileConstant fileConstant;

    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
       return execute("javac -encoding utf-8 %s\\Main.java ","java -Xmx256m -Dfile.encoding=UTF-8 -cp %s Main ", executeCodeRequest);
    }

    @Override
    public ExecuteCodeResponse execute(String compileCmd, String executeCmd, ExecuteCodeRequest executeCodeRequest) {
        executeCodeTemplate.setCompilCmd(compileCmd);
        executeCodeTemplate.setExecmdStringFormat(executeCmd);
        executeCodeTemplate.setFileTail(fileConstant.javaTailFix);
        return executeCodeTemplate.executeCode(executeCodeRequest);
    }

   /* public static void main(String[] args) {
        JavaNativeCodeSendbox javaNativeCodeSendbox = new JavaNativeCodeSendbox();
        ExecuteCodeRequest executeCodeRq = new ExecuteCodeRequest();
        String code = ResourceUtil.readStr("testCode/Main.java", StandardCharsets.UTF_8);
        executeCodeRq.setCode(code);
        executeCodeRq.setLanguage("java");
        executeCodeRq.setInputList(Arrays.asList("1 2 ", "2 3"));
        javaNativeCodeSendbox.executeCode(executeCodeRq);
    }*/
}
